package com.toKnow.service.impl;

import com.toKnow.mapper.ArticleMapper;
import com.toKnow.mapper.SourceMapper;
import com.toKnow.mapper.UserInfoMapper;
import com.toKnow.mapper.UtilMapper;
import com.toKnow.model.dto.AuthorInfoDTO;
import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.Source;
import com.toKnow.model.entity.UserInfo;
import com.toKnow.model.vo.UserInfoVO;
import com.toKnow.service.ServiceException;
import com.toKnow.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    SourceMapper sourceMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UtilMapper utilMapper;


    @Override
    public List<Article> getCollection(Integer userId, Integer page) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return articleMapper.selectArticleByCollect(userId,(page-1)*10);
    }

    @Override
    public List<Article> getArticleByAuthor(Integer userId, Integer page) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return articleMapper.selectArticleByAuthor(userId,(page-1)*10);
    }

    @Override
    public List<Article> getArticleByBrowseHistory(Integer userId, Integer page) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return articleMapper.selectArticleByBrowseHistory(userId,(page-1)*10);
    }

    @Override
    public List<Source> getSourceByAuthor(Integer userId, Integer page) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return sourceMapper.selectSourceByAuthor(userId,(page-1)*10);
    }

    @Override
    public List<Source> getSourceByBrowseHistory(Integer userId, Integer page) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        if (page==null||page<=0) throw new ServiceException("页码非法");
        return sourceMapper.selectSourceByBrowseHistory(userId,(page-1)*10);
    }

    @Override
    public List<String> getSearchHistory(Integer userId) {
        if (userId==null){
            throw new ServiceException("没有携带用户编号");
        }
        return userInfoMapper.selectSearchHistory(userId);
    }

    @Override
    public AuthorInfoDTO getAuthorInfo(Integer userId, Integer authorId) {
        UserInfo authorInfo = userInfoMapper.getAuthorInfo(authorId);
        AuthorInfoDTO authorInfoDTO = new AuthorInfoDTO(authorInfo);
        authorInfoDTO.setEducation(
            utilMapper.selectNameByEducationId(authorInfo.getEducationId())
        );
        authorInfoDTO.setUniversity(
            utilMapper.selectNameByUniversityId(authorInfo.getEducationId())
        );
        authorInfoDTO.setFollow(userInfoMapper.checkFollow(userId, authorId));
        return authorInfoDTO;
    }

    @Override
    public Boolean checkFollow(Integer userId, Integer authorId) {
        return userInfoMapper.checkFollow(userId, authorId);
    }

    @Override
    @Transactional
    public Boolean unfollow(Integer userId, Integer authorId) {
        Boolean flag = true;
        if(!userInfoMapper.unfollow(userId, authorId)) flag = false;
        if(!userInfoMapper.updateFollowNum(userId, -1)) flag = false;
        if(!userInfoMapper.updateFollowedNum(authorId, -1)) flag = false;
        return flag;
    }

    @Override
    @Transactional
    public Boolean follow(Integer userId, Integer authorId) {
        Boolean flag = true;
        if(!userInfoMapper.follow(userId, authorId)) flag = false;
        if(!userInfoMapper.updateFollowNum(userId, 1)) flag = false;
        if(!userInfoMapper.updateFollowedNum(authorId, 1)) flag = false;
        return flag;
    }

    @Override
    public List<Integer> getFollowing(Integer userId, Integer page) {
        return userInfoMapper.getFollowing(userId, (page - 1) * 10);
    }
}
